#!/bin/bash

> ~/online.log
> ~/offline.log
str="192.168.244."
for num in {1,2,5,10,133,134,135,136}
#for num in {1..255}
do

    ip=${str}${num}
    ping -c1 -w1 -i 0.2 $ip &>/dev/null
    # -c 完成次数 -w 几秒后退出 -i 间隔秒数

    status=$?
    if [ $status -eq 0 ];then
        echo -e "$ip is \e[1;32monline\e[0m"
        echo $ip >> ~/online.log
    else
        echo -e "$ip is \e[1;31moffline\e[0m"
        echo $ip >> ~/offline.log
    fi
done

online=$(awk '!a[$0]++' ~/online.log|wc -l )
offline=$(sort -u ~/offline.log|wc -l)
echo -e "当前在线的 IP 数为：${online}"
echo -e "当前不在线的 IP 数为：${offline}"
echo "当前在线的 IP 是："
cat -n ~/online.log
# awk '!a[$0]++' ~/online.log # 去重